/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller_buscarIMG;

import com.joseph.dao.StudentDaoLocal;
import com.joseph.model.Student;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 *
 * @author CEBALLOS
 */
public class BuscarIMG extends HttpServlet {

    @Resource(name = "recurso")
    private DataSource recurso;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        
        

        // String connectionURL = "jdbc:mysql://localhost:3306/studentdb"; --> conexion normal
        java.sql.Connection con;
        Statement st1;

            con = recurso.getConnection();
        String studentIdStr = request.getParameter("studentId");

        int studentId = 0;
        if (studentIdStr != null && !studentIdStr.equals("")) {
            studentId = Integer.parseInt(studentIdStr);
        }
        st1 = con.createStatement();
        ResultSet rs1 = st1.executeQuery("select foto from student where studentId='" + studentId + "' ");
        String imgLen;
        Thread t = new Thread();
        t.start();
        while (rs1.next()) {
            
            imgLen = rs1.getString(1);
            
            //     System.out.println(imgLen.length());
            int len = imgLen.length();
            
            byte[] rb = new byte[len];
            
            InputStream readImg = rs1.getBinaryStream(1);
            
             int index = readImg.read(rb, 0, len);
            //System.out.println("Byte;;;;;;;;;;;;" + index);
            //System.out.println("**************" + readImg);
            
            response.reset();
            
            response.setContentType("image/jpg");
            
            response.getOutputStream().write(rb, 0, len);/*: devuelve un objeto tipo
           
            ServletOutputStream; empleando este objeto es posible construir Servlets que
            generen respuestas binarias (por ejemplo, una imagen) y no sólo texto. */
            
            response.getOutputStream().flush();
        }
        st1.close();
        response.getOutputStream().close();
        con.close();

    }
    
   
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(BuscarIMG.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(BuscarIMG.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
